//全局变量区
var tempAlertEventWindow;
//var generalManagerId;
//var generalManagerName;
//var generalManagerJobNumber;
var idArray;
var nameArray;
var numberArray;
function putIntoArray(id,name,number){
	var i;
	for(i=0;i < idArray.length;i++){
		if(id == idArray[i]){
			return;
		}
	}
	idArray[i] = id;
	nameArray[i] = name;
	numberArray[i] = number;
}
function outputArray(){
	var text =" ";
	for(i =0;i < idArray.length;i++){
		text = text + nameArray[i] + "(" + numberArray[i] + "); ";
	}
	return text;
	
}
function transferArray(array){
	var text = "";
	for(i =0;i < array.length;i++){
		text = array[i] + "," + text;
	}
	return text;
}
Ext.newAlertEventWindow = Ext.extend(Ext.Window,{
	initialized:false,
	modal:true,
	title : '发布提醒事件',
	layout : 'fit',
	height : 440,
	width : 530,
	buttonAlign : 'center',
	buttons : [{
		xtype:'button',
  	   	text : '确认发布',
  	   	handler : function(b, e){	
		Ext.Msg.alert(transferArray(numberArray));
			var a = this.findParentBy(function(ct,cmp){
				return (ct instanceof Ext.Window) ? true : false;
			});
			var pan = a.findById("newAlertEventFormPanel");
			if(pan.form.isValid()&&pan.form.isDirty()){
				pan.form.submit({
					url : 'projectManage/projectManageAction!publishAlertEvent',
					params : {
						pageMessage1 : tempAlertEventWindow.findById("_eventTitle").getValue(),
						pageMessage2 : getLoginJobNumber(),
						pageMessage8 : transferArray(numberArray),
						pageMessage5 : tempAlertEventWindow.findById("_eventContent").getValue(),
						pageMessage6 : getLoginName(),
						pageMessage7 : transferArray(nameArray)

					},
					success : function(form,action){
						Ext.Msg.alert("发布成功","发布提醒事件成功!");
						alertEventPublishedStore.reload();
					
					},
					failure : function(form,action){
						Ext.Msg.alert("发布失败","请重新操作!");
					}
				});
			}else{
				Ext.Msg.alert("发布失败","请检查后重新操作!");
				return;
			}
//			generalManagerId = "";
//			generalManagerName = "";
//			generalManagerJobNumber = "";
			a.close();
		}
	},{
		xtype:'button',
  	   	text : '关闭',
  	   	handler : function(b, e){
			var a = this.findParentBy(function(ct,cmp){
				return (ct instanceof Ext.Window) ? true : false;
			});
//			generalManagerId = "";
//			generalManagerName = "";
//			generalManagerJobNumber = "";
			a.close();
		}
	}],
	initComponent:function(){

	idArray = new Array();
	nameArray = new Array();
	numberArray = new Array();
	tempAlertEventWindow = this;//在构造函数中，用全局变量指向新建的对象
	this.items = new Ext.form.FormPanel({
		id : 'newAlertEventFormPanel',
		layout:'form',
		height : 330,
		width : 500,
		items:[{
			id : '_eventTitle',
			xtype : 'textfield',
			width : 390,
			allowBlank : false,
			fieldLabel : '事件标题'
		},{
			id : '_eventReceiver',
			xtype : 'textfield',
			width : 390,
			allowBlank : false,
			fieldLabel : '被提醒人',
			listeners : {
				render : function(p){
				p.getEl().on('focus', function(p){
//				'select' : function(){
					var receiverWindow = new Ext.chooseEventReceiverWindow({
						buttons : [{
				        	   xtype:'button',
				        	   text : '确定',
				        	   handler : function(){
									var hasChose = receiverWindow.grid.getSelectionModel().getSelected();
									if(hasChose){
										var chooice = receiverWindow.grid.getSelectionModel().getSelections();
										var chooice_length = chooice.length;
//										var all_chooice = '';
										for(i =0;i < chooice_length;i++){
											putIntoArray(chooice[i].get("generalmanagerid"),chooice[i].get("generalmanagername"),chooice[i].get("jobnumber"));
//											all_chooice = all_chooice + chooice[i].get("generalmanagername") + "(" +chooice[i].get("jobnumber")+"); ";
//											generalManagerId = chooice[i].get("generalmanagerid") +","+ generalManagerId;
//											generalManagerName = chooice[i].get("generalmanagername")+","+ generalManagerName;
//											generalManagerJobNumber = chooice[i].get("jobnumber")+","+ generalManagerJobNumber;
										};
//										generalManagerId = chooice[0].get("generalmanagerid");
//										generalManagerName = chooice[0].get("generalmanagername");
//										generalManagerJobNumber = chooice[0].get("jobnumber");
//										var b = receiverWindow.findParentBy(function(ct,cmp){
//											return (ct instanceof Ext.form.formPanel) ? true : false;
//										});
//										var pre_chooice = tempAlertEventWindow.findById("_eventReceiver").getValue();
										tempAlertEventWindow.findById("_eventReceiver").setValue(outputArray());
										receiverWindow.close();
//										Ext.Msg.alert(generalManagerName + "(" +generalManagerId+")");
										
									}else{
										Ext.Msg.alert('选择接收者', '请选择一个接收者!');
									}
						       }
				           },{
				        	   xtype : 'button',
				        	   text : '取消',
				        	   handler : function(b, e){
				        	   		var a = this.findParentBy(function(ct,cmp){
				        	   			return (ct instanceof Ext.Window) ? true : false;
				        	   		});
				        	   		a.close();
				           	   }
				           }]
					})
					receiverWindow.grid.store.load({
						params : {
						 start : 0,
						 limit : 20
					}
					});
					receiverWindow.show();
				})
			}}
		},{
			id : '_eventContent',
			xtype : 'textarea',
			allowBlank : false,
			width : 390,
			height : 300,
			fieldLabel : '事件内容'
		}]});
	
	Ext.newAlertEventWindow.superclass.initComponent.call(this);
	
	}	
});
